package com.tjakus.parser;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import com.tjakus.adv.Advertisement;
import com.tjakus.logging.Logging;

/**
 * This class represents HTTP content parser and MIME content creator
 * 
 * @author tjakus
 * 
 */

public class HttpContentParser {

	/**
	 * This method parses string content body and returns user id
	 * 
	 */
	public static String getUserIdFromContent(String bodyData) {
		// Find user id from body data
		String userId = null;
		int firstIndex = bodyData.indexOf("f_user_id=");
		int lastIndex = bodyData.indexOf("&", firstIndex);
		userId = bodyData.substring(firstIndex + 10, lastIndex);
		Logging.getLogger().info(
				"HttpContentParser - getUserIdFromContent - [ " + userId + " ] ");
		return userId;
	}
	
	/**
	 * This method parses string content body and returns list of advertisement id
	 * 
	 */
	public static List<String> getListOfAdvertisementIdFromContent(String bodyData) {
		// Find advertisement id from body data
		List<String> listOfadvIds = new ArrayList<>();
		int firstIndex = bodyData.indexOf("<tr id=\"ad_id_");
		int lastIndex = bodyData.indexOf("\">", firstIndex);
		while (firstIndex > 0) {
			String advId = bodyData.substring(firstIndex + 14, lastIndex);
			Logging.getLogger().info(
					"HttpContentParser - getListOfAdvertisementIdFromContent - [ " + advId + " ] ");
			listOfadvIds.add(advId);
			firstIndex = bodyData.indexOf("<tr id=\"ad_id_", lastIndex);
			lastIndex = bodyData.indexOf("\">", firstIndex);
		}
		return listOfadvIds;
	}
	
	/**
	 * This method parses string content body and returns token
	 * 
	 */
	public static String getTokenFromContent(String bodyData) {
		// Find token from body data
		String token = null;
		int firstIndex = bodyData.indexOf("&input_token=");
		int lastIndex = bodyData.indexOf("'", firstIndex);
		token = bodyData.substring(firstIndex + 13, lastIndex);
		Logging.getLogger().info(
				"HttpContentParser - getTokenFromContent - [ " + token + " ] ");
		return token;
	}
	
	/**
	 * This method parses string content body and returns nonce
	 * 
	 */
	public static String getNonceFromContent(String bodyData) {
		// Find nonce from body data
		String nonce = null;
		int firstIndex = bodyData.indexOf("&nonce=");
		int lastIndex = bodyData.indexOf("'", firstIndex);
		nonce = bodyData.substring(firstIndex + 7, lastIndex);
		Logging.getLogger().info(
				"HttpContentParser - getNonceFromContent - [ " + nonce + " ] ");
		return nonce;
	}

	/**
	 * This method parses string content body and returns advertisement
	 * 
	 */
	public static Advertisement getAdvertisementDataFromContent(String bodyData,
			String advertisementId) {
		try {
			Advertisement advertisement = new Advertisement();
			Logging.getLogger().info(
					"HttpContentParser - getAdvertisementDataFromContent - [ advertisementId= "
							+ advertisementId + " ] ");
			// Set advertisement id
			advertisement.setAdvertisementId(advertisementId);
			// Find category id for this advertisement
			int firstIndex = bodyData.indexOf("current_category_id=");
			int lastIndex = bodyData.indexOf("&", firstIndex);
			advertisement.setAdvertisementCategoryId(bodyData.substring(
					firstIndex + 20, lastIndex));
			Logging.getLogger().debug(
					"HttpContentParser - getAdvertisementDataFromContent - [ categoryId= "
							+ advertisement.getAdvertisementCategoryId() + " ] ");
			// Find title for this advertisement
			firstIndex = bodyData.indexOf("fw3k_ad_input_title");
			firstIndex = bodyData.indexOf("value=\"", firstIndex);
			lastIndex = bodyData.indexOf("\"", firstIndex + 7);
			advertisement.setAdvertisementTitle(bodyData.substring(firstIndex + 7,
					lastIndex));
			Logging.getLogger().debug(
					"HttpContentParser - getAdvertisementDataFromContent - [ title= "
							+ advertisement.getAdvertisementTitle() + " ] ");
			// Find description for this advertisement
			firstIndex = bodyData.indexOf("_id_fw3k_ad_input_description");
			firstIndex = bodyData.indexOf(">", firstIndex);
			lastIndex = bodyData.indexOf("<", firstIndex);
			advertisement.setAdvertisementDescription(bodyData.substring(
					firstIndex + 1, lastIndex));
			Logging.getLogger().debug(
					"HttpContentParser - getAdvertisementDataFromContent - [ description= "
							+ advertisement.getAdvertisementDescription() + " ] ");
			// Find price for this advertisement
			firstIndex = bodyData.indexOf("fw3k_ad_input_price");
			firstIndex = bodyData.indexOf("value=\"", firstIndex);
			lastIndex = bodyData.indexOf("\"", firstIndex + 7);
			advertisement.setAdvertisementPrice(bodyData.substring(firstIndex + 7,
					lastIndex));
			Logging.getLogger().debug(
					"HttpContentParser - getAdvertisementDataFromContent - [ price= "
							+ advertisement.getAdvertisementPrice() + " ] ");
			return advertisement;
		} catch (Exception e) {
			Logging.getLogger().error(
					"HttpContentParser - getAdvertisementDataFromContent"
							+ " - [ Unable to get Advertisement detailes ] ");
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * This method creates MIME content body from advertisement
	 * 
	 */
	public static MultipartEntity createMimeContent(Advertisement advertisement) {
		try {
			Logging.getLogger().info(
					"HttpContentParser - createMimeContent - [ "
							+ advertisement.getAdvertisementTitle() + " ] ");
			MultipartEntity mimeEntity = new MultipartEntity(
					HttpMultipartMode.BROWSER_COMPATIBLE);
			mimeEntity.addPart("fw3k_ad_input_title",
					new StringBody(advertisement.getAdvertisementTitle()));
			mimeEntity.addPart("fw3k_ad_input_description", new StringBody(
					advertisement.getAdvertisementDescription()));
			mimeEntity.addPart("fw3k_ad_input_price",
					new StringBody(advertisement.getAdvertisementPrice()));
			mimeEntity.addPart("fw3k_ad_input_feature_overseas", new StringBody("0"));
			mimeEntity
					.addPart("fw3k_ad_input_additional_contact", new StringBody(""));
			mimeEntity.addPart("image_uploader_image_order_10", new StringBody(""));
			mimeEntity.addPart("ad_image_file", new StringBody(""));
			mimeEntity.addPart("fw3k_ad_input_youtube_url", new StringBody(""));
			return mimeEntity;
		} catch (UnsupportedEncodingException e) {
			Logging.getLogger().error(
					"HttpContentParser - createMimeContent -"
							+ " [ Unable to create MIME content ] ");
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * This method creates MIME content body with picture
	 * 
	 */
	public static MultipartEntity createMimeContentWithPicture(String picture) {
		Logging.getLogger().info(
				"HttpContentParser - createMimeContent - [ "
						+ picture + " ] ");
		MultipartEntity mimeEntity = new MultipartEntity(
				HttpMultipartMode.BROWSER_COMPATIBLE);
			mimeEntity.addPart("file", new FileBody(new File(picture)));
		return mimeEntity;
	}

}
